home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13160 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.6 KB  |  101 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can you printf a long
  5. Date: Thu, 04 Apr 96 21:41:30 GMT
  6. Organization: none
  7. Message-ID: <828654090snz@genesis.demon.co.uk>
  8. References: <4ju8o1$dc3@news.netam.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4ju8o1$dc3@news.netam.net>
  15.            bgc@alpha.netam.net "The Bowling Green Connection" writes:
  16.  
  17. >I had some trouble with this code:
  18. >(I'm using gcc on Digital Unix)
  19. >
  20. >int main() {
  21. >    long i;
  22. >
  23. >    while(1) {
  24. >        i++;
  25. >        printf("%f\n", i);
  26. >    }
  27. >
  28. >    return 0;
  29. >}
  30. >
  31. >The numbers printed to the screen as 0.00000.
  32. >(Nevermind that this is an infinite loop--I was piping the output to
  33. >a file which made me run out of disk space. This is just for testing)
  34. >
  35. >But when I changed %f to %d, the numbers printed correctly.
  36.  
  37. The correct format specifier for long is %ld. printf has a variable argument
  38. list which means the compiler doesn't know what the correct type for that
  39. argument is. Therefore it is up to you to match the corresponding format
  40. specifier and argument(s). If you get it wrong the behaviour is undefined -
  41. anything can happen.
  42.  
  43. Both %f printing as 0.00000 and %d printing the numbers you expected are
  44. valid actions for undefined behaviour.
  45.  
  46. >I thought that %d had the same limitations as an int..that is,
  47. >it could only print about 4 bits of information,
  48.  
  49. The C language guarantees an int can represent at least the numbers in the
  50. range -32767 to 32767 which effectively puts a minimum size of 16 bits
  51. on it. There is no upper limit. Many systems use 32 bit ints, some 64 bits,
  52. a few possibly even more.
  53.  
  54. >whereas a long
  55. >is capable of more (8 or 16 bits),
  56.  
  57. A long is required to be able to represent -2147483647 to 2147483647 i.e.
  58. effectively a minimum of 32 bits.
  59.  
  60. >so wouldn't %f (float) be able
  61. >to better handle those long numbers?
  62.  
  63. %f is the printf format specifier for a double. Since the corresponding
  64. argument is part of a variable argument list any float argument is promoted
  65. to double so %f effectively covers both floats and doubles. %lf results
  66. in undefined behaviour - don't use it.
  67.  
  68. Doubles simply have a different internal representation (and quite possibly
  69. size). It is simply interpreting the bit pattern the wrong way.
  70.  
  71. %d can write any value that an int on that system can hold. Similarly %ld
  72. can write any value that a long can hold.
  73.  
  74. > And why did %d work
  75. >correctly, even up to 634,000 (that's when my disk space ran out.. :))
  76.  
  77. Your system probably has 32 bit ints.
  78.  
  79. >Well, then I changed the "long i" declaration to "int i", and changed %f 
  80. >to %d, and I got the SAME results!  The numbers went up to 634,000!
  81. >I didn't think an int could handle this much!
  82.  
  83. You can't rely on this portably but it certainly can be true for a
  84. particular implementation.
  85.  
  86. >Another strange thing is, I did a sizeof(int) and a sizeof(long), and they
  87. >BOTH returned a 4!!!  That can't be right, can it??!
  88.  
  89. Certainly and is quite common. int can't be larger than long or smaller than
  90. short but it can be the same size. In fact implementations exist where
  91. char, short, int and long are all the same size (which of course must be
  92. at least 32 bits). In that case sizeof will report 1 for all of those types
  93. since it reports in units of a char (also called a byte in C. That can be
  94. 8 bits or more, no upper limit).
  95.  
  96. -- 
  97. -----------------------------------------
  98. Lawrence Kirby | fred@genesis.demon.co.uk
  99. Wilts, England | 70734.126@compuserve.com
  100. -----------------------------------------
  101.